package com.zhouhailin.excelutildemo.utils;

import com.monitorjbl.xlsx.StreamingReader;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

import java.io.File;
import java.io.FileInputStream;
import java.util.*;

/**
 * 读取Excel表格文件数据
 *
 * @Author：zhouhailin
 * @Date： 2023/3/8
 */
public class ReadExcel {
    public static void main(String[] args) throws Exception {
        File file = new File("D:\\BaiduSyncdisk\\公司文件\\公司项目\\电子证据平台\\南陵邮政\\快递数据对接\\违规事件列表_1678242316730.xlsx");
        long t1 = System.currentTimeMillis();
        ReadEcxcelFile(file);
        long t2 = System.currentTimeMillis();

        System.out.println("读取Excel文件花费的时间：" + (t2 - t1) / 1000 + "秒");

    }

    public static Map<String, List<String>> ReadEcxcelFile(File file) throws Exception {
        FileInputStream in = new FileInputStream(file);
        Map<String, List<String>> mapData = new HashMap<String, List<String>>();
        Workbook wk = StreamingReader.builder()
                // 缓存到内存中的行数，默认是10
                .rowCacheSize(100)
                // 读取资源时，缓存到内存的字节大小，默认是1024
                .bufferSize(4096)
                // 打开资源，必须，可以是InputStream或者是File，注意：只能打开XLSX格式的文件
                .open(in);
        int sheetNums = wk.getNumberOfSheets();
        for (int i = 0; i < sheetNums; i++) {
            List<String> sheetData = new ArrayList<String>();
            Sheet sheet = wk.getSheetAt(i);
            String sheetName = wk.getSheetName(i);
            //遍历所有的行
            int k = 0;
            for (Row row : sheet) {
                StringBuilder sb = new StringBuilder();
                //遍历所有的列
                for (Cell cell : row) {
//                    sb.append(cell.getStringCellValue());
                    System.out.print(cell.getStringCellValue() + " ");
                }
                System.out.println();
            }
        }
        return mapData;
    }
}
